package apis

import (
	"fmt"
	"strconv"
	"time"
	"vrcm/src/vrcm/app/admin/models/likes"
	"vrcm/src/vrcm/common/apis"

	"github.com/gin-gonic/gin"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"

	"vrcm/src/vrcm/app/admin/service/dto/likes"
	"vrcm/src/vrcm/app/admin/service/likes"
	"vrcm/src/vrcm/common/actions"
)

type Likes struct {
	apis.Api
}

// GetPage 获取Likes列表
// @Summary 获取Likes列表
// @Description 获取Likes列表
// @Tags Likes
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.Likes}} "{"code": 200, "data": [...]}"
// @Router /api/v1/likes [get]
// @Security Bearer
func (e Likes) GetPage(c *gin.Context) {
	req := dto.LikesGetPageReq{}
	s := service.Likes{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	p := actions.GetPermissionFromContext(c)
	list := make([]likes.Likes, 0)
	var count int64

	err = s.GetPage(&req, p, &list, &count)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取Likes失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取Likes
// @Summary 获取Likes
// @Description 获取Likes
// @Tags Likes
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.Likes} "{"code": 200, "data": [...]}"
// @Router /api/v1/likes/{id} [get]
// @Security Bearer
func (e Likes) Get(c *gin.Context) {
	req := dto.LikesGetReq{}
	s := service.Likes{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	var object likes.Likes

	p := actions.GetPermissionFromContext(c)
	err = s.Get(&req, p, &object)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取Likes失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(object, "查询成功")
}

// Insert 创建Likes
// @Summary 创建Likes
// @Description 创建Likes
// @Tags Likes
// @Accept application/json
// @Product application/json
// @Param data body dto.LikesInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/likes [post]
// @Security Bearer
func (e Likes) Insert(c *gin.Context) {
	req := dto.LikesInsertReq{}
	s := service.Likes{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	err = s.Insert(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("创建Likes失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(req.GetId(), "创建成功")
}

// Update 修改Likes
// @Summary 修改Likes
// @Description 修改Likes
// @Tags Likes
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.LikesUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/likes/{id} [put]
// @Security Bearer
func (e Likes) Update(c *gin.Context) {
	req := dto.LikesUpdateReq{}
	s := service.Likes{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	p := actions.GetPermissionFromContext(c)

	err = s.Update(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("修改Likes失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "修改成功")
}

// Delete 删除Likes
// @Summary 删除Likes
// @Description 删除Likes
// @Tags Likes
// @Param data body dto.LikesDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/likes [delete]
// @Security Bearer
func (e Likes) Delete(c *gin.Context) {
	s := service.Likes{}
	req := dto.LikesDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	// req.SetUpdateBy(user.GetUserId(c))
	p := actions.GetPermissionFromContext(c)

	err = s.Remove(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除Likes失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}

// AddLikesNum 添加点赞数到缓存
func (e Likes) AddLikesNum(c *gin.Context) {
	s := service.Likes{}
	req := dto.AddLikesNumReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	n, _ := strconv.Atoi(req.RoundId)
	if n <= 0 {
		e.Error(apis.AddLikesNumError.Code, nil, "直播场次不能为0")
		return
	}
	s.AddLikesNum(&req)

	time.Sleep(200 * time.Millisecond)
	if err != nil {
		e.Error(apis.AddLikesNumError.Code, err, fmt.Sprintf("添加点赞数到缓存失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK("", "添加点赞成功")
}

// SyncLikesNum 添加点赞数到缓存
func (e Likes) SyncLikesNum(c *gin.Context) {
	s := service.Likes{}
	err := e.MakeContext(c).
		MakeOrm().
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	s.SyncLikesNum()
	//err = s.AddLikesNum(&req)
	if err != nil {
		e.Error(apis.SyncLikesNum.Code, err, fmt.Sprintf("删除Likes失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK("", "添加成功")
}

// GetLikesNum 获取点赞数
func (e Likes) GetLikesNum(c *gin.Context) {
	s := service.Likes{}
	req := dto.GetLikesNumReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	num, err := s.GetLikesNum(&req)
	if err != nil {
		e.Error(apis.GetLikesNumError.Code, err, fmt.Sprintf("获取点赞数失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(num, "获取点赞数成功")
}
